package cn.stylefeng.guns.modular.rm;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.stylefeng.guns.modular.rm.entity.FbGame;
import cn.stylefeng.guns.modular.rm.entity.FbGameScore;
import cn.stylefeng.guns.modular.rm.entity.RmGame;
import cn.stylefeng.guns.modular.rm.entity.RmGameScore;
import cn.stylefeng.guns.modular.rm.mapper.FbGameMapper;
import cn.stylefeng.guns.modular.rm.mapper.FbGameScoreMapper;
import cn.stylefeng.guns.modular.rm.mapper.RmGameMapper;
import cn.stylefeng.guns.modular.rm.mapper.RmGameScoreMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description TODO
 * @Author xiesm
 * @Date 2024/4/17 20:25
 **/
@RestController
public class RmFbController {

    @Autowired
    private RmGameMapper rmGameMapper;
    @Autowired
    private FbGameMapper fbGameMapper;
    @Autowired
    private FbGameScoreMapper fbGameScoreMapper;
    @Autowired
    private RmGameScoreMapper rmGameScoreMapper;

    @RequestMapping("/cp/bjiao")
    public void set() {
        LambdaQueryWrapper<RmGame> lambdaQuery = new LambdaQueryWrapper<>();
        lambdaQuery.ge(RmGame::getDate, new Date())
                .lt(RmGame::getDate, DateUtil.offsetHour(new Date(), 15));

        List<RmGame> rmGames = rmGameMapper.selectList(lambdaQuery);

        List<FbGame> fbGames = fbGameMapper.selectList(null);

        for (int i = 0; i < rmGames.size(); i++) {
            boolean boo = true;
            RmGame rmgame = rmGames.get(i);
            for (int j = 0; j < fbGames.size(); j++) {
                FbGame fbgame = fbGames.get(j);
                double similarA = StrUtil.similar(rmgame.getTeamA().replace("(主)", ""), fbgame.getTeamA());
                double similarB = StrUtil.similar(rmgame.getTeamB().replace("(主)", ""), fbgame.getTeamB());
                if (similarA >= 1 || similarB >= 1) {
                    String str = StrUtil.format("league:{}\tdate:{}\trmTeamA:{}\trmTeamB:{}\tfbTeamA:{}\tfbTeamB:{}",
                            rmgame.getLeague(), DateUtil.format(rmgame.getDate(), "yyyy-MM-dd HH:mm"),
                            rmgame.getTeamA(), rmgame.getTeamB(),
                            fbgame.getTeamA(), fbgame.getTeamB());
                    print(str);
                    boo = false;

                    List<RmGameScore> rmGameScoreList = getRmGameScoreList(rmgame.getGameid());
                    List<FbGameScore> fbGameScoreList = getFbGameScoreList(fbgame.getGameid());
                    for (int k = 0; k < rmGameScoreList.size(); k++) {
                        RmGameScore rmGameScore = rmGameScoreList.get(k);
                        for (int l = 0; l < fbGameScoreList.size(); l++) {
                            FbGameScore fbGameScore = fbGameScoreList.get(l);
                            if (rmGameScore.getScore().replace(" ", "").equals(fbGameScore.getScore())
                                    && rmGameScore.getType().equals(fbGameScore.getType())
                                    && StrUtil.isNotBlank(rmGameScore.getRate())
                                    && StrUtil.isNotBlank(fbGameScore.getRate())) {
                                BigDecimal result = new BigDecimal("10000")
                                        .divide(new BigDecimal(fbGameScore.getRate()), 2)
                                        .multiply(new BigDecimal("-1"))
                                        .add(new BigDecimal(rmGameScore.getRate())
                                                .multiply(new BigDecimal("10000"))
                                                .multiply(new BigDecimal("0.9")));
                                if (result.doubleValue() > 0) {
                                    String str2 = StrUtil.format("type:{}\tscore:{}\trmRate:{}\tfbRate:{}\tresult:{}\t",
                                            rmGameScore.getType(),
                                            rmGameScore.getScore(), rmGameScore.getRate(),
                                            fbGameScore.getRate(), result.toString());
                                    print(str2);
                                }
                            }
                        }
                    }
                    continue;
                }
            }

            if (boo) {
                String str = StrUtil.format("-----------league:{},date:{},rmTeamA:{},rmTeamB:{}",
                        rmgame.getLeague(), DateUtil.format(rmgame.getDate(), "yyyy-MM-dd HH:mm"),
                        rmgame.getTeamA(), rmgame.getTeamB());
                print(str);
            }
        }


    }

    public List<RmGameScore> getRmGameScoreList(String gameid) {
        LambdaQueryWrapper<RmGameScore> query = new LambdaQueryWrapper<>();
        query = query.eq(RmGameScore::getFkRmGameId, gameid);
        return rmGameScoreMapper.selectList(query);
    }

    public List<FbGameScore> getFbGameScoreList(String gameid) {
        LambdaQueryWrapper<FbGameScore> query = new LambdaQueryWrapper<>();
        query = query.eq(FbGameScore::getFkRmGameId, gameid);
        return fbGameScoreMapper.selectList(query);
    }

    public void print(String str) {
        FileUtil.writeLines(Arrays.asList(str), "D:\\home\\rm\\result.txt", Charset.defaultCharset(), true);
    }

}
